home *** CD-ROM | disk | FTP | other *** search
- /********************************************************/
- /* */
- /* Mondrian Code from Getting Started Column */
- /* Copyright 1993, Dave Mark, All Rights Reserved */
- /* */
- /********************************************************/
-
- #include <stdio.h>
- #include <GestaltEqu.h>
-
-
- #define kMBARResID 128
- #define kErrorAlertID 128
- #define kAboutALRTid 129
-
- #define kSleep 0L
-
- #define kAutoStorage NULL
- #define kVisible true
- #define kWindowTitle "\pColor Mondrian"
- #define kMoveToFront (WindowPtr)-1
- #define kNoGoAway false
- #define kNULLRefCon 60L
-
- #define mApple 128
- #define iAbout 1
-
- #define mFile 129
- #define iQuit 1
-
- #define mDevice 131
-
- #define kWindowMargin 5
-
- #define kRandomUpperLimit 32768
-
- #define kEmptyString "\p"
- #define kNULLFilterProc NULL
-
-
- /*************/
- /* Globals */
- /*************/
-
- Boolean gDone;
-
-
- /***************/
- /* Functions */
- /***************/
-
- void ToolboxInit( void );
- void MenuBarInit( void );
- void CreateWindow( GDHandle device );
- void EventLoop( void );
- void DoEvent( EventRecord *eventPtr );
- void HandleMouseDown( EventRecord *eventPtr );
- void HandleMenuChoice( long menuChoice );
- void HandleAppleChoice( short item );
- void HandleFileChoice( short item );
- void HandleDeviceChoice( short item );
- Boolean HasColorQD( void );
- GDHandle GetDeepestDevice( void );
- short GetDeviceDepth( GDHandle device );
- void DrawRandomRect( void );
- void RandomColor( RGBColor *colorPtr );
- void RandomRect( Rect *rectPtr );
- short Randomize( short range );
- void DoError( Str255 errorString );
-
-
- /****************** main ***************************/
-
- void main( void )
- {
- ToolboxInit();
-
- if ( ! HasColorQD() )
- DoError( "\pThis machine doesn't support Color Quickdraw!" );
-
- MenuBarInit();
-
- CreateWindow( GetDeepestDevice() );
-
- EventLoop();
- }
-
-
- /****************** ToolboxInit *********************/
-
- void ToolboxInit( void )
- {
- InitGraf( &thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( nil );
- InitCursor();
- }
-
-
- /****************** MenuBarInit ***********************/
-
- void MenuBarInit( void )
- {
- Handle menuBar;
- MenuHandle menu;
- GDHandle device, deepestDevice;
- Str255 itemStr;
- short curDeviceNumber = 1;
-
- menuBar = GetNewMBar( kMBARResID );
- SetMenuBar( menuBar );
-
- menu = GetMHandle( mApple );
- AddResMenu( menu, 'DRVR' );
-
- menu = GetMHandle( mDevice );
-
- deepestDevice = GetDeepestDevice();
-
- device = GetDeviceList();
-
- while ( device != NULL )
- {
- itemStr[0] = 10;
- sprintf( (char *)(&(itemStr[1])), "0x%08lX", (unsigned long)device );
- AppendMenu( menu, itemStr );
-
- if ( device == deepestDevice )
- CheckItem( menu, curDeviceNumber, true );
-
- device = GetNextDevice( device );
- curDeviceNumber++;
- }
-
- DrawMenuBar();
- }
-
-
- /****************** CreateWindow ***********************/
-
- void CreateWindow( GDHandle device )
- {
- WindowPtr window;
- Rect wBounds;
-
- wBounds = (**device).gdRect;
-
- if ( device == GetMainDevice() )
- wBounds.top += GetMBarHeight();
-
- InsetRect( &wBounds, kWindowMargin, kWindowMargin );
-
- window = NewCWindow( kAutoStorage,
- &wBounds,
- kWindowTitle,
- kVisible,
- altDBoxProc,
- kMoveToFront,
- kNoGoAway,
- kNULLRefCon );
-
- if ( window == nil )
- {
- DoError( "\pCouldn't create window!" );
- }
- else
- {
- ShowWindow( window );
- SetPort( window );
- }
- }
-
-
- /******************************** EventLoop *********/
-
- void EventLoop( void )
- {
- EventRecord event;
-
- GetDateTime( (unsigned long *)(&randSeed) );
-
- gDone = false;
- while ( gDone == false )
- {
- if ( WaitNextEvent( everyEvent, &event, kSleep, nil ) )
- DoEvent( &event );
-
- DrawRandomRect();
- }
- }
-
-
- /************************************* DoEvent */
-
- void DoEvent( EventRecord *eventPtr )
- {
- char theChar;
-
- switch ( eventPtr->what )
- {
- case mouseDown:
- HandleMouseDown( eventPtr );
- break;
- case keyDown:
- case autoKey:
- theChar = eventPtr->message & charCodeMask;
- if ( (eventPtr->modifiers & cmdKey) != 0 )
- HandleMenuChoice( MenuKey( theChar ) );
- break;
- }
- }
-
-
- /************************************* HandleMouseDown */
-
- void HandleMouseDown( EventRecord *eventPtr )
- {
- WindowPtr window;
- short thePart;
- long menuChoice;
-
- thePart = FindWindow( eventPtr->where, &window );
-
- switch ( thePart )
- {
- case inMenuBar:
- menuChoice = MenuSelect( eventPtr->where );
- HandleMenuChoice( menuChoice );
- break;
- case inSysWindow :
- SystemClick( eventPtr, window );
- break;
- }
- }
-
-
- /****************** HandleMenuChoice ***********************/
-
- void HandleMenuChoice( long menuChoice )
- {
- short menu;
- short item;
-
- if ( menuChoice != 0 )
- {
- menu = HiWord( menuChoice );
- item = LoWord( menuChoice );
-
- switch ( menu )
- {
- case mApple:
- HandleAppleChoice( item );
- break;
- case mFile:
- HandleFileChoice( item );
- break;
- case mDevice:
- HandleDeviceChoice( item );
- break;
- }
- HiliteMenu( 0 );
- }
- }
-
-
- /****************** HandleAppleChoice ***********************/
-
- void HandleAppleChoice( short item )
- {
- MenuHandle appleMenu;
- Str255 accName;
- short accNumber;
-
- switch ( item )
- {
- case iAbout:
- NoteAlert( kAboutALRTid, kNULLFilterProc );
- break;
- default:
- appleMenu = GetMHandle( mApple );
- GetItem( appleMenu, item, accName );
- accNumber = OpenDeskAcc( accName );
- break;
- }
- }
-
-
- /****************** HandleFileChoice ***********************/
-
- void HandleFileChoice( short item )
- {
- switch ( item )
- {
- case iQuit :
- gDone = true;
- break;
- }
- }
-
-
- /****************** HandleDeviceChoice ***********************/
-
- void HandleDeviceChoice( short item )
- {
- /* Try this: Modify the program so that when a device is selected
- from the Device menu, the current window gets closed and a
- new window is opened on the selected device. Be careful when
- you translate the menu item back into an address. Debug your
- program thoroughly before you try to use the address as an
- address. You don't want to accidentally reformat your hard
- drive, right?
-
- Also, don't forget to update the check mark!
- */
- }
-
-
- /****************** HasColorQD *****************/
-
- Boolean HasColorQD( void )
- {
- unsigned char version[ 4 ];
- OSErr err;
-
- err = Gestalt( gestaltQuickdrawVersion, (long *)version );
-
- if ( err != noErr )
- {
- SysBeep( 10 ); /* Error calling Gestalt!!! */
- ExitToShell();
- }
-
- if ( version[ 2 ] > 0 )
- return( true );
- else
- return( false );
- }
-
-
- /****************** GetDeepestDevice *****************/
-
- GDHandle GetDeepestDevice( void )
- {
- GDHandle curDevice, maxDevice = NULL;
- short curDepth, maxDepth = 0;
-
- curDevice = GetDeviceList();
-
- while ( curDevice != NULL )
- {
- curDepth = GetDeviceDepth( curDevice );
-
- if ( curDepth > maxDepth )
- {
- maxDepth = curDepth;
- maxDevice = curDevice;
- }
-
- curDevice = GetNextDevice( curDevice );
- }
-
- return( maxDevice );
- }
-
-
- /****************** GetDeviceDepth *****************/
-
- short GetDeviceDepth( GDHandle device )
- {
- PixMapHandle screenPixMapH;
-
- screenPixMapH = (**device).gdPMap;
-
- return( (**screenPixMapH).pixelSize );
- }
-
-
- /****************** DrawRandomRect *****************/
-
- void DrawRandomRect( void )
- {
- Rect randomRect;
- RGBColor color;
-
- RandomRect( &randomRect );
- RandomColor( &color );
- RGBForeColor( &color );
- PaintOval( &randomRect );
- }
-
-
- /****************** RandomColor *********************/
-
- void RandomColor( RGBColor *colorPtr )
- {
- colorPtr->red = Random() + 32767;
- colorPtr->blue = Random() + 32767;
- colorPtr->green = Random() + 32767;
- }
-
-
- /****************** RandomRect *********************/
-
- void RandomRect( Rect *rectPtr )
- {
- WindowPtr window;
-
- window = FrontWindow();
-
- rectPtr->left = Randomize( window->portRect.right
- - window->portRect.left );
- rectPtr->right = Randomize( window->portRect.right
- - window->portRect.left );
- rectPtr->top = Randomize( window->portRect.bottom
- - window->portRect.top );
- rectPtr->bottom = Randomize( window->portRect.bottom
- - window->portRect.top );
- }
-
-
- /****************** Randomize **********************/
-
- short Randomize( short range )
- {
- long randomNumber;
-
- randomNumber = Random();
-
- if ( randomNumber < 0 )
- randomNumber *= -1;
-
- return( (randomNumber * range) / kRandomUpperLimit );
- }
-
-
- /***************** DoError ********************/
-
- void DoError( Str255 errorString )
- {
- ParamText( errorString, kEmptyString, kEmptyString, kEmptyString );
-
- StopAlert( kErrorAlertID, kNULLFilterProc );
- }